package com.javaclimb.music.controller;

import com.javaclimb.music.domain.Singer;
import com.javaclimb.music.service.SingerService;
import com.javaclimb.music.utils.PageUtils;
import com.javaclimb.music.utils.R;
import com.javaclimb.music.vo.SingerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

/**
 * @author 夏天
 * @date 2020年11月22日 12:33
 */
@Api(tags = "歌手接口")
@RequestMapping("/singer")
@RestController
@Slf4j
public class SingerController {

    @Autowired
    SingerService singerService;

    @ApiOperation("修改或添加歌手")
    @PostMapping("/saveOrUpdate")
    public R addSinger(@RequestBody SingerVo singerVo) {
        Singer singer = new Singer();
        BeanUtils.copyProperties(singerVo, singer);
        if (singerService.saveOrUpdate(singer)) {
            return R.ok();
        }
        return R.error("添加或保存歌手失败");
    }

    @ApiOperation("删除歌手")
    @PostMapping("/delete")
    public R deleteSinger(@RequestBody Integer[] ids) {
        if (singerService.removeByIds(Arrays.asList(ids))) {
            return R.ok();
        }
        return R.error("删除歌手失败");
    }

    @ApiOperation("获取歌手信息")
    @GetMapping("/info/{id}")
    public R getSinger(@PathVariable("id") Integer id) {
        Singer singer = singerService.getById(id);
        if (singer != null) {
            return R.ok().put("info", singer);
        }
        return R.error("获取歌手信息失败");
    }

    @ApiOperation("获取所以歌手信息")
    @GetMapping("/list")
    public R listSinger(@RequestParam Map<String, Object> params) {
        PageUtils pageUtils = singerService.queryPage(params);
        return R.ok().put("data", pageUtils);
    }

    @ApiOperation("更新歌手图片")
    @PostMapping("/updateSingerPic")
    public R updateSingerPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id") int id) {
        if (avatorFile == null || avatorFile.isEmpty()) {
            return R.error("图片上次失败");
        }
        //文件名=当前时间戳+源文件名
        String fileName = System.currentTimeMillis() + avatorFile.getOriginalFilename();
        //文件路径
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator")
                + "img" + System.getProperty("file.separator")
                + "singerPic";
        //如果路径不存在则创建文件夹
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        //实际文件的地址
        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        //存储到数据库里的相对稳健地址
        String storeAvatarPath = "/img/singerPic/" + fileName;
        try {
            avatorFile.transferTo(dest);
            Singer singer = new Singer();
            singer.setId(id);
            singer.setPic(storeAvatarPath);
            if (singerService.updateById(singer)) {
                return R.ok("上次成功");
            }
        } catch (IOException e) {
            log.error("上传图片失败", e);
            return R.error("上传失败");
        }
        log.error("上传图片失败：可能是修改数据表图片信息出现问题");
        return R.error("修改失败");
    }


    /**
     * 查询歌手数量
     */
    @GetMapping(value = "/allSinger")
    public Object allSinger() {
        return R.ok().put("data", singerService.count());
    }

}
